package work.mediway.mdm.biz.entity.hierarchy;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mediway.hos.app.base.seure.util.SecurityUtils;
import com.mediway.hos.common.annotation.Excel;

import java.io.Serializable;

import javax.validation.constraints.NotBlank;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import work.mediway.mdm.core.enums.StatusEnum;
import work.mediway.mdm.core.util.DUtil;

/**
 * 组织机构树
 *
 * @author ruoyi
 * @since 2022-08-25
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("HOS_Organization")
@ApiModel(value = "HosOrganization对象", description = "组织机构树")
public class HosOrganization implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(name = "组织代码(必填)")
    @ApiModelProperty(value = "组织代码")
    @TableId(value = "ORG_Code", type = IdType.INPUT)
    private String orgCode;

    @Excel(name = "组织名称(必填)")
    @NotBlank(message = "不能为空")
    @ApiModelProperty(value = "组织名称", required = true)
    @TableField("ORG_Desc")
    private String orgDesc;

    @Excel(name = "统一社会信用代码")
    @ApiModelProperty(value = "统一社会信用代码")
    @TableField("ORG_USCC")
    private String orgUscc;

    @Excel(name = "组织简称")
    @ApiModelProperty(value = "组织简称")
    @TableField("ORG_Abbrev")
    private String orgAbbrev;

    @Excel(name = "组织简介")
    @ApiModelProperty(value = "组织简介")
    @TableField("ORG_Intro")
    private String orgIntro;

    @Excel(name = "组织类型")
    @ApiModelProperty(value = "组织类型")
    @TableField("ORG_OMCCode")
    private String orgOmcCode;

    @Excel(name = "组织法人")
    @ApiModelProperty(value = "组织法人")
    @TableField("ORG_LegalEntity")
    private String orgLegalEntity;

    @Excel(name = "组织负责人")
    @ApiModelProperty(value = "组织负责人")
    @TableField("ORG_Header")
    private String orgHeader;

    @Excel(name = "上级组织代码")
    @ApiModelProperty(value = "上级组织代码")
    @TableField(value = "ORG_ORGCode", updateStrategy = FieldStrategy.IGNORED)
    private String orgOrgCode;

    @Excel(name = "组织级次")
    @ApiModelProperty(value = "组织级次")
    @TableField("ORG_ORGHCode")
    private String orgOrghCode;

    @Excel(name = "组织分类")
    @ApiModelProperty(value = "组织分类")
    @TableField("ORG_ORGCCode")
    private String orgOrgcCode;

    @Excel(name = "组织子类")
    @ApiModelProperty(value = "组织子类")
    @TableField("ORG_ORGSCCode")
    private String orgOrgscCode;

    @Excel(name = "组织小类")
    @ApiModelProperty(value = "组织小类")
    @TableField("ORG_ORGMCCode")
    private String orgOrgmcCode;

    @Excel(name = "组织专业分类")
    @ApiModelProperty(value = "组织专业分类")
    @TableField("ORG_ORGSPCCode")
    private String orgOrgspcCode;

    @Excel(name = "组织自主类型(必填)")
    @NotBlank(message = "不能为空")
    @ApiModelProperty(value = "组织自主类型", required = true)
    @TableField("ORG_ORGICCode")
    private String orgOrgicCode;

    @Excel(name = "组织行政类型")
    @ApiModelProperty(value = "组织行政类型")
    @TableField("ORG_ORGACCode")
    private String orgOrgacCode;

    @Excel(name = "组织所在国家")
    @ApiModelProperty(value = "组织所在国家")
    @TableField("ORG_CountryCode")
    private String orgCountryCode;

    @Excel(name = "组织所在省")
    @ApiModelProperty(value = "组织所在省")
    @TableField("ORG_PROVCode")
    private String orgProvCode;

    @Excel(name = "组织所在市")
    @ApiModelProperty(value = "组织所在市")
    @TableField("ORG_CITYCode")
    private String orgCityCode;

    @Excel(name = "组织所在区")
    @ApiModelProperty(value = "组织所在区")
    @TableField("ORG_DISTRCode")
    private String orgDistrCode;

    @Excel(name = "组织地址")
    @ApiModelProperty(value = "组织地址")
    @TableField("ORG_Address")
    private String orgAddress;

    @Excel(name = "组织邮编")
    @ApiModelProperty(value = "组织邮编")
    @TableField("ORG_ZipCode")
    private String orgZipCode;

    @Excel(name = "联系电话")
    @ApiModelProperty(value = "联系电话")
    @TableField("ORG_Tel")
    private String orgTel;

    @Excel(name = "传真")
    @ApiModelProperty(value = "传真")
    @TableField("ORG_Fax")
    private String orgFax;

    @Excel(name = "邮件")
    @ApiModelProperty(value = "邮件")
    @TableField("ORG_Email")
    private String orgEmail;

    @Excel(name = "官网")
    @ApiModelProperty(value = "官网")
    @TableField("ORG_WebSite")
    private String orgWebSite;

    @Excel(name = "建立日期")
    @ApiModelProperty(value = "建立日期")
    @TableField("ORG_FoundDate")
    private String orgFoundDate;

    @Excel(name = "注销日期")
    @ApiModelProperty(value = "注销日期")
    @TableField("ORG_CancelDate")
    private String orgCancelDate;

    @Excel(name = "是否有效(必填)")
    @NotBlank(message = "不能为空")
    @ApiModelProperty(value = "是否有效", required = true)
    @TableField("ORG_Activity")
    private String orgActivity;

    @Excel(name = "开始日期")
    @ApiModelProperty(value = "开始日期")
    @TableField("ORG_StartDate")
    private String orgStartDate;

    @Excel(name = "结束日期")
    @ApiModelProperty(value = "结束日期")
    @TableField("ORG_EndDate")
    private String orgEndDate;

    @Excel(name = "系统创建日期(必填)")
    @ApiModelProperty(value = "系统创建日期", hidden = true)
    @TableField("ORG_CreateDate")
    private String orgCreateDate;

    @Excel(name = "系统创建时间(必填)")
    @ApiModelProperty(value = "系统创建时间", hidden = true)
    @TableField("ORG_CreateTime")
    private String orgCreateTime;

    @Excel(name = "系统创建人(必填)")
    @ApiModelProperty(value = "系统创建人", hidden = true)
    @TableField("ORG_CreateUser")
    private String orgCreateUser;

    @Excel(name = "系统顺序号")
    @ApiModelProperty(value = "系统顺序号")
    @TableField("ORG_SeqNo")
    private Integer orgSeqNo;

    @Excel(name = "系统最后更新日期(必填)")
    @ApiModelProperty(value = "系统最后更新日期", hidden = true)
    @TableField("ORG_LastUpdateDate")
    private String orgLastUpdateDate;

    @Excel(name = "系统最后更新时间(必填)")
    @ApiModelProperty(value = "系统最后更新时间", hidden = true)
    @TableField("ORG_LastUpdateTime")
    private String orgLastUpdateTime;

    @Excel(name = "系统最后更新人(必填)")
    @ApiModelProperty(value = "系统最后更新人", hidden = true)
    @TableField("ORG_LastUpdateUser")
    private String orgLastUpdateUser;

    @Excel(name = "拼音码")
    @ApiModelProperty(value = "拼音码")
    @TableField("ORG_PYCode")
    private String orgPyCode;

    @Excel(name = "五笔码")
    @ApiModelProperty(value = "五笔码")
    @TableField("ORG_WBCode")
    private String orgWbCode;

    @Excel(name = "备注")
    @ApiModelProperty(value = "备注")
    @TableField("ORG_Mark")
    private String orgMark;

    @ApiModelProperty(value = "职位数量", hidden = true)
    @TableField(exist = false)
    private Long positionNum = 0L;

    @ApiModelProperty(value = "职位数量", hidden = true)
    @TableField(exist = false)
    private Long personNum = 0L;


    public static void setCreateHosOrganization(HosOrganization hosOrganization) {
        hosOrganization.setOrgCreateUser(SecurityUtils.getLoginName());
        hosOrganization.setOrgCreateDate(DUtil.splitNow()[0]);
        hosOrganization.setOrgCreateTime(DUtil.splitNow()[1]);
        hosOrganization.setOrgActivity(StrUtil.isBlank(hosOrganization.getOrgActivity()) ? StatusEnum.USE_FLAG_TRUE.getStatusCode() : hosOrganization.getOrgActivity());
        hosOrganization.setOrgStartDate(StrUtil.isNotBlank(hosOrganization.getOrgStartDate()) ? hosOrganization.getOrgStartDate() : DateUtil.today());
        hosOrganization.setOrgEndDate(StrUtil.isNotBlank(hosOrganization.getOrgEndDate()) ? hosOrganization.getOrgEndDate() : null);
        setUpdateHosOrganization(hosOrganization);
    }

    public static void setUpdateHosOrganization(HosOrganization hosOrganization) {
        if (StrUtil.isBlank(hosOrganization.getOrgOrgCode())) {
            hosOrganization.setOrgOrgCode(null);
        }
        hosOrganization.setOrgLastUpdateDate(DUtil.splitNow()[0]);
        hosOrganization.setOrgLastUpdateTime(DUtil.splitNow()[1]);
        hosOrganization.setOrgLastUpdateUser(SecurityUtils.getLoginName());
        hosOrganization.setOrgStartDate(StrUtil.isNotBlank(hosOrganization.getOrgStartDate()) ? hosOrganization.getOrgStartDate() : DateUtil.today());
        hosOrganization.setOrgEndDate(StrUtil.isNotBlank(hosOrganization.getOrgEndDate()) ? hosOrganization.getOrgEndDate() : null);
    }
}
